package seesman.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import defiant.common.web.DefiantServlet;

/**
 * Servlet implementation class LoginServlet
 */
public final class LoginServlet extends DefiantServlet {
	Logger log = Logger.getLogger(this.getClass());
	
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Template t = Velocity.getTemplate("s/login.htm");
			VelocityContext ctx = getVelocityContext(request, response);
			
			// sample only
			ctx.put("user", "joseph acapulco");
						
			PrintWriter writer = response.getWriter();
			t.merge(ctx, writer);
			
		} catch (Exception e) {
			log.error(e);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO: do login validation
		
//		if (user+pw == success)
		
		// forward to console window
		//request.getRequestDispatcher("console").forward(request, response);
		response.sendRedirect("console");
	}

}
